import { relations } from "drizzle-orm/relations";
import { accountGroup, accountGroupUser, user, bank, bankCard, incomeExpenseDetail, incomeExpenseDetailType, joinAccountGroupRequest, journey, region, journeyIncomeExpenseDetail, role, userRole } from "./schema";

export const accountGroupUserRelations = relations(accountGroupUser, ({one}) => ({
	accountGroup: one(accountGroup, {
		fields: [accountGroupUser.accountGroupId],
		references: [accountGroup.id]
	}),
	user: one(user, {
		fields: [accountGroupUser.userId],
		references: [user.id]
	}),
}));

export const accountGroupRelations = relations(accountGroup, ({many}) => ({
	accountGroupUsers: many(accountGroupUser),
	bankCards: many(bankCard),
	incomeExpenseDetails: many(incomeExpenseDetail),
	incomeExpenseDetailTypes: many(incomeExpenseDetailType),
	joinAccountGroupRequests: many(joinAccountGroupRequest),
	journeys: many(journey),
	journeyIncomeExpenseDetails: many(journeyIncomeExpenseDetail),
	users: many(user),
	userRoles: many(userRole),
}));

export const userRelations = relations(user, ({one, many}) => ({
	accountGroupUsers: many(accountGroupUser),
	joinAccountGroupRequests_receiverId: many(joinAccountGroupRequest, {
		relationName: "joinAccountGroupRequest_receiverId_user_id"
	}),
	joinAccountGroupRequests_ownerId: many(joinAccountGroupRequest, {
		relationName: "joinAccountGroupRequest_ownerId_user_id"
	}),
	joinAccountGroupRequests_senderId: many(joinAccountGroupRequest, {
		relationName: "joinAccountGroupRequest_senderId_user_id"
	}),
	accountGroup: one(accountGroup, {
		fields: [user.accountGroupId],
		references: [accountGroup.id]
	}),
	userRoles: many(userRole),
}));

export const bankCardRelations = relations(bankCard, ({one, many}) => ({
	bank: one(bank, {
		fields: [bankCard.bankId],
		references: [bank.id]
	}),
	accountGroup: one(accountGroup, {
		fields: [bankCard.accountGroupId],
		references: [accountGroup.id]
	}),
	incomeExpenseDetails: many(incomeExpenseDetail),
	journeyIncomeExpenseDetails: many(journeyIncomeExpenseDetail),
}));

export const bankRelations = relations(bank, ({many}) => ({
	bankCards: many(bankCard),
}));

export const incomeExpenseDetailRelations = relations(incomeExpenseDetail, ({one}) => ({
	accountGroup: one(accountGroup, {
		fields: [incomeExpenseDetail.accountGroupId],
		references: [accountGroup.id]
	}),
	bankCard: one(bankCard, {
		fields: [incomeExpenseDetail.bankCardId],
		references: [bankCard.id]
	}),
}));

export const incomeExpenseDetailTypeRelations = relations(incomeExpenseDetailType, ({one}) => ({
	accountGroup: one(accountGroup, {
		fields: [incomeExpenseDetailType.accountGroupId],
		references: [accountGroup.id]
	}),
}));

export const joinAccountGroupRequestRelations = relations(joinAccountGroupRequest, ({one}) => ({
	user_receiverId: one(user, {
		fields: [joinAccountGroupRequest.receiverId],
		references: [user.id],
		relationName: "joinAccountGroupRequest_receiverId_user_id"
	}),
	user_ownerId: one(user, {
		fields: [joinAccountGroupRequest.ownerId],
		references: [user.id],
		relationName: "joinAccountGroupRequest_ownerId_user_id"
	}),
	user_senderId: one(user, {
		fields: [joinAccountGroupRequest.senderId],
		references: [user.id],
		relationName: "joinAccountGroupRequest_senderId_user_id"
	}),
	accountGroup: one(accountGroup, {
		fields: [joinAccountGroupRequest.accountGroupId],
		references: [accountGroup.id]
	}),
}));

export const journeyRelations = relations(journey, ({one, many}) => ({
	accountGroup: one(accountGroup, {
		fields: [journey.accountGroupId],
		references: [accountGroup.id]
	}),
	region: one(region, {
		fields: [journey.regionId],
		references: [region.id]
	}),
	journeyIncomeExpenseDetails: many(journeyIncomeExpenseDetail),
}));

export const regionRelations = relations(region, ({many}) => ({
	journeys: many(journey),
}));

export const journeyIncomeExpenseDetailRelations = relations(journeyIncomeExpenseDetail, ({one}) => ({
	journey: one(journey, {
		fields: [journeyIncomeExpenseDetail.journeyId],
		references: [journey.id]
	}),
	bankCard: one(bankCard, {
		fields: [journeyIncomeExpenseDetail.bankCardId],
		references: [bankCard.id]
	}),
	accountGroup: one(accountGroup, {
		fields: [journeyIncomeExpenseDetail.accountGroupId],
		references: [accountGroup.id]
	}),
}));

export const userRoleRelations = relations(userRole, ({one}) => ({
	role: one(role, {
		fields: [userRole.roleId],
		references: [role.id]
	}),
	user: one(user, {
		fields: [userRole.userId],
		references: [user.id]
	}),
	accountGroup: one(accountGroup, {
		fields: [userRole.accountGroupId],
		references: [accountGroup.id]
	}),
}));

export const roleRelations = relations(role, ({many}) => ({
	userRoles: many(userRole),
}));